from access.core.utterance import Utterance
from third_party.active_state.recipes.autoassign import autoassign
import django.template

class UtteranceGenerator:
  @autoassign
  def __init__(self,
               rendering_context = {}): pass

  def Generate(self, pattern):
    if not pattern.context \
    or not len(pattern.context):
      return [Utterance(pattern.value_pattern,
                        pattern.snippet_ID_pattern)]
    
    #TODO see if we need extra validation for context / anything else.
    infos = [] #HACK Fix this later. Basically need to calculate combinations.

    for key in pattern.context:
      for value in pattern.context[key]:
        self.rendering_context.update({key: value})
        context = django.template.Context(self.rendering_context)
        value = django.template.Template\
        (pattern.value_pattern).render(context)
        snippet_ID = django.template.Template\
        (pattern.snippet_ID_pattern).render(context)

        infos.append(Utterance(value, snippet_ID))
    return infos
